package com.sjsu.CMPE226.etl.parser;

/**
 * Parsing of weather data files.
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.sjsu.CMPE226.etl.utils.DateUtils;
import com.sjsu.CMPE226.normalized.jdbc.data.Observation;

/**
 * @author Pawar Deepali
 * 
 */
public class JDBCWeatherOutFileParser {

	public List<Observation> parseString(String dataStr) {
		Scanner scanner = new Scanner(dataStr);
		List<Observation> weatherDataList = new ArrayList<Observation>();
		boolean isHeaderLineProcess = false;
		while (scanner.hasNextLine()) {

			String line = scanner.nextLine().trim();
			String[] values = line.split("[ ]+");
			if (values == null || values.length < 15)
				continue;
			if (!isHeaderLineProcess) {
				// skip header line
				isHeaderLineProcess = true;
				continue;
			}

			Observation w = new Observation();
			setObservationValues(w, values);
			weatherDataList.add(w);

		}
		return weatherDataList;

	}

	public void setObservationValues(Observation obs, String[] values) {
		if (values.length > 15) {
			obs.setStationId(values[0]);
			obs.setObservationsDateTime(DateUtils
					.convertStringToDate(values[1]));
			obs.setTemperature(Float.valueOf(values[6]));
			obs.setWindSpeed(Float.valueOf(values[7]));
			obs.setDirection(Float.valueOf(values[8]));
			obs.setGust(Float.valueOf(values[9]));
			obs.setSeaLevelPressure(Float.valueOf(values[10]));
			obs.setAltimeter(Float.valueOf(values[11]));
			obs.setDewPoint(Float.valueOf(values[12]));
			obs.setRelativeHumidity(Float.valueOf(values[13]));
		} else {
			System.out.println("Error in row data insufficient, skipping");
		}
	}

}
